package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    public static final String USER_IN_SESSION ="USER_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION ="EXPRESSION_IN_SESSION";

    public static HttpSession getSession(){
        //RequestContextHolder 可以在任意的地方获取request对象,response对象,session对象
        ServletRequestAttributes requestAttributes =(ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpSession session = requestAttributes.getRequest().getSession();
        return session;
    }

    //往session存入登录用户
    public static void setCurrentUser(Employee employee){

        getSession().setAttribute(USER_IN_SESSION,employee);
    }
    //获取当前登录员工
    public static Employee getCurrentUser(){
        return (Employee) getSession().getAttribute(USER_IN_SESSION);
    }


    /**
     * 获取当前登录用户的权限表达式
     * @return
     */
    public static List<String> getExpression(){
        return (List<String>)getSession().getAttribute(EXPRESSION_IN_SESSION);
    }

    /**
     * 设置当前登录员工的权限表达式
     * @param expression
     */
    public static void setExpression(List<String> expression){
        getSession().setAttribute(EXPRESSION_IN_SESSION,expression);
    }
}
